package domain.cron;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.data.QnAPersistenceManager;
import domain.model.Domain;
import domain.service.AgeDomain;
import domain.service.AlexaUpdate;
import domain.service.CheckRobotSitemap;
import domain.service.GooglePageRank;
import domain.service.Ip;
import domain.service.SelfSiteUpdate;

@SuppressWarnings("serial")
public class CronUpdateServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		Query query = psm.newQuery(Domain.class);
		query.setFilter("lastUpdateDate==null");
		query.setRange(0,1);
		@SuppressWarnings("unchecked")
		List<Domain> domains = (List<Domain>) query.execute();
		if(domains.size()>0)
		{
			String title_url = domains.get(0).getName();
			new AgeDomain(title_url);
			new Ip(title_url);
			new GooglePageRank(title_url);
			new domain.service.Whois(title_url);
			new SelfSiteUpdate(title_url);
			new CheckRobotSitemap(title_url);
			
			@SuppressWarnings("unchecked")
			List<Domain> check =  (List<Domain>)psm.newQuery(Domain.class,"name == '"+title_url+"'").execute();
		    
		    if(check.get(0).getDescription()!= null && check.get(0).getDescription().equals("") || check.get(0).getDescription() ==  null)
		    {
			    new SelfSiteUpdate("www."+title_url);
		    }
			new AlexaUpdate(title_url);
		}
	}

}
